package com.bestcem.xm.component.mybatis.configure;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @ClassName: JacksonObjectMapperConfig
 * @Description:
 * @Author: chenglong.yue
 * @Date: 2023/2/23 16:18
 */
@Configuration
@AutoConfigureAfter(JacksonAutoConfiguration.class)
public class JacksonObjectMapperConfig {

    public static final String LOCAL_DATE_TIME_FORMAT = "yyyy/MM/dd HH:mm:ss";
    public static final String LOCAL_DATE_FORMAT = "yyyy/MM/dd";
    public static final String LOCAL_TIME_FORMAT = "HH:mm:ss";

    //@Bean
    //@Scope("prototype")
    //Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder() {
    //    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    //    //builder.serializationInclusion(JsonInclude.Include.ALWAYS);
    //    //builder.propertyNamingStrategy(SNAKE_CASE);
    //    //builder.featuresToEnable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    //    //builder.featuresToEnable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS);
    //    //builder.featuresToEnable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    //
    //    builder.locale(Locale.CHINA);
    //    builder.timeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
    //    builder.simpleDateFormat(LOCAL_DATE_TIME_FORMAT);
    //    builder.modules(javaTimeModule());
    //    return builder;
    //}


    @Bean
    @Primary
    @Scope("prototype")
    public ObjectMapper objectMapper() {
        return new ObjectMapper()
                //.setSerializationInclusion(JsonInclude.Include.ALWAYS)
                //.setPropertyNamingStrategy(SNAKE_CASE)
                .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
                .configure(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS, false)
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                .setLocale(Locale.CHINA)
                .setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()))
                .setDateFormat(new SimpleDateFormat(LOCAL_DATE_TIME_FORMAT))
                .registerModule(javaTimeModule());
    }

    public Module javaTimeModule() {
        JavaTimeModule module = new JavaTimeModule();

        module.addSerializer(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(LOCAL_DATE_TIME_FORMAT)));
        module.addSerializer(new LocalDateSerializer(DateTimeFormatter.ofPattern(LOCAL_DATE_FORMAT)));
        module.addSerializer(new LocalTimeSerializer(DateTimeFormatter.ofPattern(LOCAL_TIME_FORMAT)));

        module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(LOCAL_DATE_TIME_FORMAT)));
        module.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(LOCAL_DATE_FORMAT)));
        module.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(LOCAL_TIME_FORMAT)));
        return module;
    }
}
